package cn.iocoder.yudao.module.trade.service.receiptinfo;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.trade.controller.admin.receiptinfo.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.receiptinfo.ReceiptInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.trade.dal.mysql.receiptinfo.ReceiptInfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*;

/**
 * 单据信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ReceiptInfoServiceImpl implements ReceiptInfoService {

    @Resource
    private ReceiptInfoMapper receiptInfoMapper;

    @Override
    public Long createReceiptInfo(ReceiptInfoSaveReqVO createReqVO) {
        // 插入
        ReceiptInfoDO receiptInfo = BeanUtils.toBean(createReqVO, ReceiptInfoDO.class);
        receiptInfoMapper.insert(receiptInfo);
        // 返回
        return receiptInfo.getId();
    }

    @Override
    public void updateReceiptInfo(ReceiptInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateReceiptInfoExists(updateReqVO.getId());
        // 更新
        ReceiptInfoDO updateObj = BeanUtils.toBean(updateReqVO, ReceiptInfoDO.class);
        receiptInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteReceiptInfo(Long id) {
        // 校验存在
        validateReceiptInfoExists(id);
        // 删除
        receiptInfoMapper.deleteById(id);
    }

    private void validateReceiptInfoExists(Long id) {
        if (receiptInfoMapper.selectById(id) == null) {
            throw exception(RECEIPT_INFO_NOT_EXISTS);
        }
    }

    @Override
    public ReceiptInfoDO getReceiptInfo(Long id) {
        return receiptInfoMapper.selectById(id);
    }

    @Override
    public PageResult<ReceiptInfoDO> getReceiptInfoPage(ReceiptInfoPageReqVO pageReqVO) {
        return receiptInfoMapper.selectPage(pageReqVO);
    }

}